﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;

using Dx.SqlAccess;
using Dx.Common;


namespace Dx.SysAdmin
{
    /// <summary>
    ///模块编号：CS002
    ///功能描述：实现系统参数接口
    ///创 建 人：杨秀东
    ///创建日期：2008-10-24
    /// </summary>
    public class SysParam:ISysParam
    {
        /// <summary>
        /// 编辑系统参数
        /// </summary>
        private const string Pro_Update_SysParam = "Pro_Update_SysParam";

        /// <summary>
        /// 获取系统参数
        /// </summary>
        private const string Sql_Select_SysParam = "SELECT * FROM SysParam";

        #region ISysParam 成员

        /// <summary>
        /// 获取系统参数到实体
        /// </summary>
        /// <returns>SysParamInfo，null</returns>
        public SysParamInfo GetSysParamInfo()
        {
            SysParamInfo paramInfo = new SysParamInfo();
            SqlDataReader dr = null;

            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                dr = SqlDataAccess.ExecuteReader(conn, CommandType.Text, Sql_Select_SysParam, null);
                if (dr.Read())
                {
                    paramInfo.Aqsz = int.Parse(dr["Aqsz"].ToString());
                    paramInfo.Dxfl = Convert.ToDecimal(dr["Dxfl"].ToString());
                    paramInfo.Kcxs = Convert.ToDecimal(dr["Kcxs"].ToString());

                    paramInfo.Qjfs = int.Parse(dr["Qjfs"].ToString());
                    paramInfo.Qtcs = dr["Qtcs"].ToString();
                    paramInfo.Xslc = int.Parse(dr["Xslc"].ToString());

                    paramInfo.Xsws = int.Parse(dr["Xsws"].ToString());
                    paramInfo.Xtwh = int.Parse(dr["Xtwh"].ToString());
                    paramInfo.Xxms = int.Parse(dr["Xxms"].ToString());
                    paramInfo.Xxjg = int.Parse(dr["Xxjg"].ToString());

                    paramInfo.Yhzz = int.Parse(dr["Yhzz"].ToString());
                    paramInfo.Ymdz = dr["Ymdz"].ToString();
                    paramInfo.id = int.Parse(dr["id"].ToString());

                    paramInfo.zc = int.Parse(dr["zc"].ToString());
                    paramInfo.jzc = int.Parse(dr["jzc"].ToString());
                    paramInfo.kmzc = int.Parse(dr["kmzc"].ToString());
                    paramInfo.kmjzc = int.Parse(dr["kmjzc"].ToString());
                    paramInfo.kmshow = int.Parse(dr["kmshow"].ToString());

                    paramInfo.Mzxz = int.Parse(dr["mzxz"].ToString());
                    paramInfo.Sfdm = int.Parse(dr["sfdm"].ToString());
                    paramInfo.TspKey = dr["tspkey"].ToString();
                    paramInfo.IsDaiMai = int.Parse(dr["IsDaiMai"].ToString());
                    paramInfo.YktExchangeType = int.Parse(dr["YktExchangeType"].ToString());
                    paramInfo.Zhye = decimal.Parse(dr["Zhye"].ToString());

                    if ((dr["zzqr"].ToString() == "1") || (dr["zzqr"].ToString().ToLower() == "true"))
                    {
                        paramInfo.zzqr = true;
                    }
                    else
                    {
                        paramInfo.zzqr = false;
                    }
                    //add by lxzh 2009-9-9
                    paramInfo.ExportPosition = int.Parse(dr["ExportPosition"].ToString());
                    paramInfo.ExportTime = int.Parse(dr["ExportTime"].ToString());

                    //add by lxzh 2010-3-4
                    paramInfo.PayByHandHint = dr["PayByHandHint"].ToString();
                    paramInfo.TTZXSiteName = dr["TTZXSiteName"].ToString();
                    paramInfo.TTYKTSiteName = dr["TTYKTSiteName"].ToString();
                    paramInfo.TTWDSiteName = dr["TTWDSiteName"].ToString();
                    paramInfo.TTZXTimeOut = int.Parse(dr["TTZXTimeOut"].ToString());
                    paramInfo.StorageWarm = int.Parse(dr["StorageWarm"].ToString());
                    if (dr["StorageChangeBuyPrice"].ToString() == "1" || dr["StorageChangeBuyPrice"].ToString().ToLower() == "true")
                        paramInfo.StorageChangeBuyPrice = true;
                    else
                        paramInfo.StorageChangeBuyPrice = false;

                    if (dr["TTZXRemoveSafe"].ToString() == "1" || dr["TTZXRemoveSafe"].ToString().ToLower() == "true")
                        paramInfo.TTZXRemoveSafe = true;
                    else
                        paramInfo.TTZXRemoveSafe = false;

                    if (dr["TTZXRemoveXJSafe"].ToString() == "1" || dr["TTZXRemoveXJSafe"].ToString().ToLower() == "true")
                        paramInfo.TTZXRemoveXJSafe = true;
                    else
                        paramInfo.TTZXRemoveXJSafe = false;

                    paramInfo.VIPDayWarning = int.Parse(dr["VIPDayWarning"].ToString());
                    paramInfo.VIPMoneyWarning = decimal.Parse(dr["VIPMoneyWarning"].ToString());
                    paramInfo.AgentDefaultIndex = int.Parse(dr["AgentDefaultIndex"].ToString());

                    paramInfo.IsProOffMsg = int.Parse(dr["IsProOffMsg"].ToString());
                    paramInfo.SendMail = dr["SendMail"].ToString();
                    paramInfo.IsAutoUpPro = int.Parse(dr["IsAutoUpPro"].ToString());
                    paramInfo.AutoUpDownProTime = int.Parse(dr["AutoUpDownProTime"].ToString());
                    paramInfo.OrderSuspendTime = int.Parse(dr["OrderSuspendTime"].ToString());
                    paramInfo.ThirdOrderAreaMode = int.Parse(dr["ThirdOrderAreaMode"].ToString());
                    paramInfo.HandBuyAreaId = int.Parse(dr["HandBuyAreaId"].ToString());
                    paramInfo.IsThirdSale = int.Parse(dr["IsThirdSale"].ToString());

                    //add by lxzh 
                    if (dr["UsingSecurity"].ToString() == "1" || dr["UsingSecurity"].ToString().ToLower() == "true")
                        paramInfo.UsingSecurity = true;
                    else
                        paramInfo.UsingSecurity = false;
                    if (dr["IsVerifyYard"].ToString() == "1" || dr["IsVerifyYard"].ToString().ToLower() == "true")
                        paramInfo.IsVerifyYard = true;
                    else
                        paramInfo.IsVerifyYard = false;
                    if (dr["IsUserIP"].ToString() == "1" || dr["IsUserIP"].ToString().ToLower() == "true")
                        paramInfo.IsUserIP = true;
                    else
                        paramInfo.IsUserIP = false;
                    if (dr["IsSafeCode"].ToString() == "1" || dr["IsSafeCode"].ToString().ToLower() == "true")
                        paramInfo.IsSafeCode = true;
                    else
                        paramInfo.IsSafeCode = false;
                    if (dr["IsHardInfo"].ToString() == "1" || dr["IsHardInfo"].ToString().ToLower() == "true")
                        paramInfo.IsHardInfo = true;
                    else
                        paramInfo.IsHardInfo = false;
                    if (dr["IsCard"].ToString() == "1" || dr["IsCard"].ToString().ToLower() == "true")
                        paramInfo.IsCard = true;
                    else
                        paramInfo.IsCard = false;
                    if (dr["IsOTP"].ToString() == "1" || dr["IsOTP"].ToString().ToLower() == "true")
                        paramInfo.IsOTP = true;
                    else
                        paramInfo.IsOTP = false;
                }
                else
                {
                    paramInfo = null;
                }
                conn.Close();
            }

            // 返回结果
            return paramInfo;
        }

        /// <summary>
        /// 获取系统参数到数据集
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet GetSysParam()
        {
            DataSet ds = new DataSet();
            // 执行语句
            ds = SqlDataAccess.GetDataSet(SqlDataAccess.SQLConnString1, CommandType.Text, Sql_Select_SysParam, null);
            return ds;
        }

        /// <summary>
        /// 更新系统参数
        /// </summary>
        /// <param name="paramInfo">参数实体</param>
        /// <returns>true,false</returns>
        public bool EditSysParm(SysParamInfo paramInfo)
        {
            int intNum = 0;

            // 定义参数
            ParameterCollection param = new ParameterCollection();
            param.Add("@Aqsz", paramInfo.Aqsz);
            param.Add("@UpdateSafeUserSet", paramInfo.UpdateSafeUserSet);
            param.Add("@Dxfl", paramInfo.Dxfl);
            //@param.Add("@Kcxs", paramInfo.Kcxs);
            //param.Add("@Qjfs", paramInfo.Qjfs);
            //@param.Add("@Qtcs", paramInfo.Qtcs);
            //param.Add("@Xslc", paramInfo.Xslc);
            //param.Add("@Xsws", paramInfo.Xsws);
            param.Add("@Xtwh", paramInfo.Xtwh);
            //param.Add("@Xxms", paramInfo.Xxms);
            param.Add("@Yhzz", paramInfo.Yhzz);
            //param.Add("@Ymdz", paramInfo.Ymdz);
            param.Add("@Mzxz", paramInfo.Mzxz);
            param.Add("@Xxjg", paramInfo.Xxjg);

            param.Add("@zc", paramInfo.zc);
            param.Add("@jzc", paramInfo.jzc);
            param.Add("@kmzc", paramInfo.kmzc);
            param.Add("@kmjzc", paramInfo.kmjzc);
            param.Add("@kmshow", paramInfo.kmshow);
            param.Add("@zzqr", paramInfo.zzqr);

            param.Add("@tspkey", paramInfo.TspKey);
            param.Add("@isDaiMai", paramInfo.IsDaiMai);
            param.Add("@YktExchangeType", paramInfo.YktExchangeType);
            param.Add("@Zhye", paramInfo.Zhye);
            param.Add("@Sfdm", paramInfo.Sfdm);

            //add by lxzh 2010-3-4
            param.Add("@TTZXSiteName", paramInfo.TTZXSiteName);
            param.Add("@TTYKTSiteName", paramInfo.TTYKTSiteName);
            param.Add("@TTWDSiteName", paramInfo.TTWDSiteName);
            param.Add("@PayByHandHint", paramInfo.PayByHandHint);
            param.Add("@TTZXTimeOut", paramInfo.TTZXTimeOut);
            param.Add("@StorageWarm", paramInfo.StorageWarm);
            param.Add("@StorageChangeBuyPrice", paramInfo.StorageChangeBuyPrice);
            param.Add("@TTZXRemoveSafe", paramInfo.TTZXRemoveSafe);
            param.Add("@TTZXRemoveXJSafe", paramInfo.TTZXRemoveXJSafe);
            param.Add("@VIPDayWarning", paramInfo.VIPDayWarning);
            param.Add("@VIPMoneyWarning", paramInfo.VIPMoneyWarning);

            param.Add("@IsProOffMsg", paramInfo.IsProOffMsg);
            param.Add("@SendMail", paramInfo.SendMail);

            param.Add("@IsAutoUpPro", paramInfo.IsAutoUpPro);
            param.Add("@AutoUpDownProTime", paramInfo.AutoUpDownProTime);
            param.Add("@OrderSuspendTime", paramInfo.OrderSuspendTime);
            
            //add by lxzh 2011-10-22
            param.Add("@UsingSecurity", paramInfo.UsingSecurity);
            param.Add("@IsVerifyYard", paramInfo.IsVerifyYard);
            param.Add("@IsUserIP", paramInfo.IsUserIP);
            param.Add("@IsHardInfo", paramInfo.IsHardInfo);
            param.Add("@IsCard", paramInfo.IsCard);
            param.Add("@IsSafeCode", paramInfo.IsSafeCode);
            param.Add("@IsOTP", paramInfo.IsOTP);
            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    intNum = SqlDataAccess.ExecuteNonQuery(conn, CommandType.StoredProcedure, Pro_Update_SysParam, param);
                }
                catch (Exception ex)
                {
                    intNum = 0;
                }
                finally
                {
                    conn.Close();
                }
            }
            //if (intNum == 1)
            if (intNum >= 1)
                return true;
            else
                return false;
        }

        #endregion






        //更新一个参数的值
        public int UpdateCanShuValue(string system,string CanShuName,string CanShuValue)
        {
            int rtn = 0;

            if (string.IsNullOrEmpty(CanShuName) || string.IsNullOrEmpty(system))
            {
                return -1;
            }

            string ls_sql = " update sysCanshu set CanshuValue = @CanShuValue where SystemID = @SystemID and CanshuName = @CanShuName ";

            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@SystemID",SqlDbType.NVarChar,20),
                new SqlParameter("@CanShuName",SqlDbType.NVarChar,50),
                new SqlParameter("@CanShuValue",SqlDbType.NVarChar,50)
            };

            param[0].Value = system;
            param[1].Value = CanShuName;
            param[2].Value = CanShuValue;


            // 执行语句
            using (SqlConnection conn = new SqlConnection(SqlDataAccess.SQLConnString1))
            {
                try
                {
                    return SqlDataAccess.ExecuteNonQueryWx(conn, CommandType.Text, ls_sql, param);

                }
                catch
                {
                    return -1;
                }
                finally
                {
                    conn.Close();
                }
            }
        }


        //根据参数名查询参数值
        public DataSet GetCanShu()
        {

            try
            {
                string ls_sql = " select * from sysCanshu ";

                Dx.SqlAccess.ParameterCollection param = null;

                // 执行语句
                DataSet ds = SqlDataAccess.GetDataSet(SqlDataAccess.SQLConnString1, CommandType.Text, ls_sql, param);

                return ds;
            }
            catch
            {
                return null;
            }

        }


    }
}
